
// FovDrivenRadiosity.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "FovDrivenRadiosity.h"
#include "FovDrivenRadiosityDlg.h"
#include <Radiosity\Parameters.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CFovDrivenRadiosityApp

BEGIN_MESSAGE_MAP(CFovDrivenRadiosityApp, CWinApp)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CFovDrivenRadiosityApp construction

CFovDrivenRadiosityApp::CFovDrivenRadiosityApp()
{
	// support Restart Manager
	m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

	// Place all significant initialization in InitInstance
	_parameters = new Parameters();
	_registryKey = CString("Texas A&M University");
	_section = CString("FovDrivenRadiosity");
	_windowWidth = CString("Window Width");
	_windowHeight = CString("Window Height");
	_radiosityAlgorithm = CString("Radiosity Algorithm");
	_ambient = CString("Ambient");
	_filename = CString("Filename");
	_subdivisionLevel = CString("Subdivision Level");
	_itemBufferResolution = CString("Item Buffer Resolution");
	_fullscreen = CString("Fullscreen");
	_visualization = CString("Visualization");
	_fps = CString("FPS");
	_shootLevel = CString("Shoot Level");
	_fovAngle = CString("Fov Angle");
	_fovDecayInterval = CString("Fov Decay Interval");
	_unshotMipmapShootLevel = CString("Unshot Mipmap Shoot Level");

	CString cameraName[] = { CString("Main View"), CString("Dual View") };
	for(int i = 0; i < 2; i++)
	{
		_cameraPosition[i] = cameraName[i] + CString(" Position");
		_cameraAim[i] = cameraName[i] + CString(" Aim");
		_cameraUp[i] = cameraName[i] + CString(" Up");
	}
	_dualView = CString("Dual View");
}

CFovDrivenRadiosityApp::~CFovDrivenRadiosityApp()
{
	delete _parameters;
}


// The one and only CFovDrivenRadiosityApp object

CFovDrivenRadiosityApp theApp;


// CFovDrivenRadiosityApp initialization

BOOL CFovDrivenRadiosityApp::InitInstance()
{
	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinApp::InitInstance();

	// Standard initialization
	SetRegistryKey(_registryKey);
	ReadParameters();

	CFovDrivenRadiosityDlg dlg;
	m_pMainWnd = &dlg;
	dlg.DoModal();

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

void CFovDrivenRadiosityApp::ReadParameters(void)
{
	int winWidth = GetProfileInt(_section, _windowWidth, _parameters->GetWindowWidth());
	int winHeight = GetProfileInt(_section, _windowHeight, _parameters->GetWindowHeight());
	int radiosityAlgorithm = GetProfileInt(_section, _radiosityAlgorithm, _parameters->GetRadiosityAlgorithm());
	BOOL useAmbient = GetProfileInt(_section, _ambient, _parameters->GetDisplayAmbient());
	CStringA filename = CStringA(GetProfileString(_section, _filename, CString(_parameters->GetMeshFilename().c_str())));
	int subdivisionLevel = GetProfileInt(_section, _subdivisionLevel, _parameters->GetSubdivisionLevel());
	int itemBufferResolution = GetProfileInt(_section, _itemBufferResolution, _parameters->GetItemBufferResolution());
	BOOL fullscreen = GetProfileInt(_section, _fullscreen, _parameters->GetDisplayFullscreen());
	BOOL visualization = GetProfileInt(_section, _visualization, _parameters->GetDisplayVisualization());
	int fps = GetProfileInt(_section, _fps, _parameters->GetRequestedFPS());
	int shootLevel = GetProfileInt(_section, _shootLevel, _parameters->GetShootLevel());
	int fovAngle = GetProfileInt(_section, _fovAngle, _parameters->GetFovAngle());
	int fovDecayInterval = GetProfileInt(_section, _fovDecayInterval, _parameters->GetFovDecayInterval());
	int unshotMipmapShootLevel = GetProfileInt(_section, _unshotMipmapShootLevel, _parameters->GetUnshotMipmapShootLevel());
	BOOL dualView = GetProfileInt(_section, _dualView, _parameters->GetDualView());

	Vector3d *cameraPosition, *cameraAim , *cameraUp;
	UINT n;
	for(int i = 0; i < 2; i++)
	{
		GetProfileBinary(_section, _cameraPosition[i], (LPBYTE*)&cameraPosition, &n);
		if(n > 0)
		{
			_parameters->SetCameraPosition(i, *cameraPosition);
		}
		GetProfileBinary(_section, _cameraAim[i], (LPBYTE*)&cameraAim, &n);
		if(n > 0)
		{
			_parameters->SetCameraAim(i, *cameraAim);
		}
		GetProfileBinary(_section, _cameraUp[i], (LPBYTE*)&cameraUp, &n);
		if(n > 0)
		{
			_parameters->SetCameraUp(i, *cameraUp);
		}
	}

	_parameters->SetWindowWidth(winWidth);
	_parameters->SetWindowHeight(winHeight);
	_parameters->SetRadiosityAlgorithm((RadiosityAlgorithm)radiosityAlgorithm);
	_parameters->SetDisplayAmbient(useAmbient != 0);
	_parameters->SetMeshFilename(std::string(filename));
	_parameters->SetSubdivisionLevel(subdivisionLevel);
	_parameters->SetItemBufferResolution(itemBufferResolution);
	_parameters->SetDisplayFullscreen(fullscreen != 0);
	_parameters->SetDisplayVisualization(visualization != 0);
	_parameters->SetRequestedFPS(fps);
	_parameters->SetShootLevel(shootLevel);
	_parameters->SetFovAngle(fovAngle);
	_parameters->SetFovDecayInterval(fovDecayInterval);
	_parameters->SetUnshotMipmapShootLevel(unshotMipmapShootLevel);
	_parameters->SetDualView(dualView != 0);
}

void CFovDrivenRadiosityApp::WriteParameters(void)
{
	WriteProfileInt(_section, _windowWidth, _parameters->GetWindowWidth());
	WriteProfileInt(_section, _windowHeight, _parameters->GetWindowHeight());
	WriteProfileInt(_section, _radiosityAlgorithm, _parameters->GetRadiosityAlgorithm());
	WriteProfileInt(_section, _ambient, _parameters->GetDisplayAmbient());
	WriteProfileString(_section, _filename, CString(_parameters->GetMeshFilename().c_str()));
	WriteProfileInt(_section, _subdivisionLevel, _parameters->GetSubdivisionLevel());
	WriteProfileInt(_section, _itemBufferResolution, _parameters->GetItemBufferResolution());
	WriteProfileInt(_section, _fullscreen, _parameters->GetDisplayFullscreen());
	WriteProfileInt(_section, _visualization, _parameters->GetDisplayVisualization());
	WriteProfileInt(_section, _fps, _parameters->GetRequestedFPS());
	WriteProfileInt(_section, _shootLevel, _parameters->GetShootLevel());
	WriteProfileInt(_section, _fovAngle, _parameters->GetFovAngle());
	WriteProfileInt(_section, _fovDecayInterval, _parameters->GetFovDecayInterval());
	WriteProfileInt(_section, _unshotMipmapShootLevel, _parameters->GetUnshotMipmapShootLevel());
	for(int i = 0; i < 2; i++)
	{
		Vector3d cameraPosition = _parameters->GetCameraPosition(i);
		Vector3d cameraAim = _parameters->GetCameraAim(i);
		Vector3d cameraUp = _parameters->GetCameraUp(i);
		WriteProfileBinary(_section, _cameraPosition[i], (LPBYTE)&cameraPosition, sizeof(Vector3d));
		WriteProfileBinary(_section, _cameraAim[i], (LPBYTE)&cameraAim, sizeof(Vector3d));
		WriteProfileBinary(_section, _cameraUp[i], (LPBYTE)&cameraUp, sizeof(Vector3d));
	}
	WriteProfileInt(_section, _dualView, _parameters->GetDualView());
}
